module BootstrapPaginateHelper

  def will_paginate(collection_or_options = nil, options = {})
    if collection_or_options.is_a? Hash
      options, collection_or_options = collection_or_options, nil
    end
    unless options[:renderer]
      options = options.merge :renderer => PaginateLinkRenderer
    end
    super *[collection_or_options, options].compact
  end

  class  PaginateLinkRenderer < WillPaginate::ActionView::LinkRenderer

    def container_attributes
      super.except(:first_label, :last_label)
    end

    protected

    def pagination
      items = @options[:page_links] ? windowed_page_numbers : []
      items.unshift :previous_page
      items.unshift :first_page
      items.push :next_page
      items.push :last_page
    end

    def first_page
      previous_or_next_page(current_page == 1 ? nil : 1, @options[:first_label], "first_page")
    end

    def last_page
      previous_or_next_page(current_page == total_pages ? nil : total_pages, @options[:last_label], "last_page")
    end

  end

end
